package com.dy.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.dy.common.core.domain.AjaxResult;
import com.dy.framework.config.mqtt.MqttAcceptClient;
import com.dy.framework.config.mqtt.MqttSendClient;
import com.dy.system.domain.FreshMedicalMachine;
import com.dy.system.service.IFreshMedicalMachineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/initMqtt")
public class MqttController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IFreshMedicalMachineService freshMedicalMachineService;
    @Autowired
    private MqttSendClient MqttSendClient;
    @Autowired
    private MqttAcceptClient mqttAcceptClient;



    @GetMapping(value = "/send/msg")
    public Object publishTopic(String node) {
        Map<String,Object> accessDataMap = new HashMap<>();
        accessDataMap.put("Unix",System.currentTimeMillis());
        accessDataMap.put("Version","V1.0");
        accessDataMap.put("Content","alldata");
        MqttSendClient.publish(false,node+"12345678/access_data",JSON.toJSONString(accessDataMap));
        return null;
    }

    @PostMapping("/op/medical")
    public AjaxResult opMedical(@RequestParam String code, @RequestParam String command, @RequestParam String commandValue){
        //发送消息
        Map<String,Object> writeDataMap = new HashMap<>();
        writeDataMap.put("Unix",System.currentTimeMillis());
        writeDataMap.put("Version","V1.0");
        Map<String,Object> medicalMap = new HashMap<>();
        Map<String,Object> commandMap = new HashMap<>();
        commandMap.put(command,commandValue);
        medicalMap.put("RXDQ",commandMap);
        writeDataMap.put("Write_Data",medicalMap);
        logger.info("accessDataMap ={}",JSON.toJSONString(writeDataMap));
        MqttSendClient.publish(false,code+"12345678/access_data", JSON.toJSONString(writeDataMap));
        return AjaxResult.success();
    }


    /***
     * 订阅某个主题
     *
     * @param topicName
     * @param qos
     * @return
     */
    @GetMapping("/sub/topic")
    public String subscribeTopic(String topicName,int qos){
        mqttAcceptClient.subscribe(topicName,qos);
        return "success";
    }

    @GetMapping("/sub/all/topic")
    public String subAll(){
        List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
        for (FreshMedicalMachine medicalMachine : machineList) {
            String topicName = medicalMachine.getMachineCode()+"12345678/pub_data";
            logger.info("subscribe topicName ={}",topicName);
            mqttAcceptClient.subscribe(topicName,0);
        }
        return "success";
    }


    @GetMapping("/start")
    public AjaxResult initSubscribeTopic()throws Exception{
        List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
        for (FreshMedicalMachine medicalMachine : machineList) {
            //发送消息
            Map<String,Object> accessDataMap = new HashMap<>();
            accessDataMap.put("Unix",System.currentTimeMillis());
            accessDataMap.put("Version","V1.0");
            accessDataMap.put("Content","alldata");
            logger.info("accessDataMap ={}", JSON.toJSONString(accessDataMap));
            MqttSendClient.publish(false,medicalMachine.getMachineCode()+"12345678/access_data", JSON.toJSONString(accessDataMap));
        }
        return AjaxResult.success();
    }



}
